﻿using API.Core.ActionResults;
using API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace API.Controllers
{
    public class ProfileController : CommonControllerAsync
    {
        readonly API.Core.IEventProvider _eventProvider;
        readonly API.Models.Repository.Sessions_RepositoryAsync _sessionRepository;
        readonly API.Models.Repository.Users_RepositoryAsync _userRepository;

        public ProfileController()
        {
            _sessionRepository = new Models.Repository.Sessions_RepositoryAsync();
            _userRepository = new Models.Repository.Users_RepositoryAsync();
        }

        public async Task<CommonEdexResult<API_SESSION2>> Login(string email = null, string password = null, string api_key = null)
        {
            var _sessionRepository = new Models.Repository.Sessions_RepositoryAsync();

            var ip = Request.RequestContext.HttpContext.Request.UserHostAddress;
            var data = String.IsNullOrEmpty(api_key)
                ? await _sessionRepository.LogIn(email, password, ip)
                : await _sessionRepository.LogIn(api_key, ip);

            var api_session = new API.Models.API_SESSION2
            {
                API_KEY = data.APIKEY,
                LOGON_DATETIME = data.LOGON_DATETIME
            };

            return new CommonEdexResult<API_SESSION2>(api_session);
        }

        public async Task<CommonEdexResult> LogOut()
        {
            var _sessionRepository = new Models.Repository.Sessions_RepositoryAsync();
            await _sessionRepository.LogOut(this._api_key, this._ip);

            return new CommonEdexResult();
        }

        [HttpPost]
        public async Task<CommonEdexResult> Register(API.Models.SEC_USERS user)
        {
            await _userRepository.Register(user);
            return new CommonEdexResult();
        }

        public async Task<CommonEdexResult> ChangePassword(string password)
        {
            var session = await _sessionRepository.GetSession(_api_key, _ip);
            _userRepository.SetSession(session);

            await _userRepository.ChangePassword(password);
            return new CommonEdexResult();
        }

        public async Task<CommonEdexResult> ResetPassword(string email)
        {
            var genPasswordLength = int.Parse(System.Configuration.ConfigurationManager.AppSettings["genPasswordLength"]);
            var password = API.Core.Util.PasswordGenerator(genPasswordLength, false);

            await _userRepository.ResetPassword(email, password);
            return new CommonEdexResult();
        }

        [HttpPost]
        public async Task<CommonEdexResult> ChangeUserInfo(API.Models.SEC_USERS user)
        {
            var session = await _sessionRepository.GetSession(_api_key, _ip);
            _userRepository.SetSession(session);

            await _userRepository.Edit(user);
            return new CommonEdexResult();
        }

        public async Task<CommonEdexResult> ChangeCompany(Guid COMPANY_GUID)
        {
            await _sessionRepository.ChangeCompany(this._api_key, this._ip, COMPANY_GUID);
            return new CommonEdexResult();
        }

        public async Task<CommonEdexResult<SEC_USERS>> GetSessionUser()
        {
            var data = await _sessionRepository.GetUser(this._api_key, this._ip);
            return new CommonEdexResult<SEC_USERS>(data);
        }
        
        public async Task<CommonEdexResult<WH_COMPANIES>> GetSessionCompany()
        {
            var data = await _sessionRepository.GetCompany(this._api_key, this._ip);
            return new CommonEdexResult<WH_COMPANIES>(data);
        }

    }


}
